import json
import time

from flask import Flask as _Flask,jsonify
from flask import request
from flask import render_template
from flask.json import JSONEncoder as _JSONEncoder

import decimal
import DataBase_online_MYSQL
import DataBase_redis



class JSONEncoder(_JSONEncoder):
        def default(self, o):
            if isinstance(o, decimal.Decimal):
                return float(o)
            super(_JSONEncoder, self).default(o)



class Flask(_Flask):
    json_encoder = JSONEncoder


# app = Flask(__name__)
app = Flask(__name__,template_folder='templates',static_folder="static")

# @app.route('/hello')
# def hello_world():
#     return 'Hello World!'
#
# @app.route('/login')
# def hello_world2():
#     name = request.values.get("name")
#     pwd = request.values.get("pwd")
#     return f'name={name}.pwd={pwd}'
#
# @app.route('/login')#登录界面提交
# def hello_world1():
#     id = request.values.get("id")
#     return f"""
#     <form action="/login">
#             账号：<input name="name"><br>
#             密码：<input name="pwd">
#             <input type="submit">
#         </form>
#     """
# @app.route('/add/student/',methods=['post'])
# def add_stu():
#     if  not request.data:   #检测是否有数据
#         return ('fail')
#     student = request.data.decode('utf-8')
#     #获取到POST过来的数据，因为我这里传过来的数据需要转换一下编码。根据晶具体情况而定
#     student_json = json.loads(student)
#     #把区获取到的数据转为JSON格式。
#     return jsonify(student_json)
#     #返回JSON数据。
# @app.route('/ajax',methods=["get","post"])#ajax是刷新数据用的
# def hello_word4():
#     return '10000'


@app.route('/')#主页
def hello_word3():
    return render_template("main.html")

@app.route('/page2')
def hello_word2():
    return render_template("page2.html")

@app.route('/page3')
def hello_word1():
    return render_template("page3.html")


#剩下的都是api接口了，单纯返回json数据的


@app.route('/time')
def get_time():
    return time.strftime("%Y{}%m{}%d{} %X").format("年","月","日")

@app.route('/c1')#c1是中间第一个表格
def get_c1_data():
    data = DataBase_online_MYSQL.get_c1_data()
    return jsonify({"data":data[0]})

@app.route('/beijing')#中间第二个（地图）
def get_beijing_json():
    with open(r"static\js\beijing.json", 'r') as load_f:
        load_dict = json.load(load_f)
    return jsonify(load_dict)

@app.route('/l1')
def get_l1_data():
    # data = utilsB.get_l1_data()
    # x,y = [],[]
    # for a,b in data[0:]:
    #     x.append(a)
    #     y.append(b)
    # return jsonify({"x": x, "y": y})
    data=DataBase_redis.get_web_l1_data()
    return jsonify(data)


@app.route('/l2')
def get_l2_data():
    # data = utilsB.get_l2_data()
    # x,y = [],[]
    # for a,b in data[0:]:
    #     x.append(a)
    #     y.append(b)
    # return jsonify({"x":x,"y":y})
    data=DataBase_redis.get_web_l2_data()
    return jsonify(data)

@app.route('/r1')
def get_r1_data():
    data = DataBase_redis.get_fpga_last_data_from_redis()
    return jsonify({"y":[data]})

@app.route('/r2')
def get_r2_data():
    data = DataBase_redis.get_fpga_last_data_from_redis()
    return jsonify({"y":[data]})

host="127.0.0.1"
port=5000


def app_run(flaskPort):
    global port
    port=flaskPort
    app.run(host,port)

if __name__ == '__main__':
    app.run(host,port)
